package com.zhuhjay.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.vod.prop.VodProperties;
import com.zhuhjay.vod.service.VodService;
import com.zhuhjay.vod.utils.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZhuHJay
 * @date 2022/9/7 14:14
 */
@Api(description = "阿里云视频播放控制器")
@RestController
@RequestMapping("/eduvod/video")

public class VodController {

    @Resource
    private VodService vodService;

    @Resource
    private VodProperties vodProperties;

    @ApiOperation(value = "视频上传到阿里云")
    @PostMapping("uploadAlyiVideo")
    public R uploadAlyVideo(MultipartFile file) {
        return R.ok().data("videoId", vodService.uploadVideoAly(file));
    }

    @ApiOperation(value = "删除单个视频")
    @DeleteMapping("removeAlyVideo/{videoId}")
    public R removeAlyVideo(@PathVariable String videoId) {
        vodService.removeVideoAly(videoId);
        return R.ok();
    }

    @ApiOperation(value = "批量删除视频")
    @DeleteMapping("removeAllAlyVideo")
    public R removeAllAlyVideo(@RequestParam("videoList") List<String> videoList) {
        vodService.removeAllVideoAly(videoList);
        return R.ok();
    }

    @ApiOperation(value = "获取视频播放凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getVideoPlayAuth(@PathVariable String id) throws Exception {
        DefaultAcsClient client = InitVodClient
                .initVodClient(vodProperties.getKeyId(), vodProperties.getKeySecret());

        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

        request.setVideoId(id);
        response = client.getAcsResponse(request);

        return R.ok().data("playAuth", response.getPlayAuth());
    }


}
